<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/4/27
 * Time: 11:26
 */
namespace Admin\Controller;

use Admin\Api\AddressApi;
use Admin\Api\MemberApi;
use Shop\Api\MemberConfigApi;
use Shop\Api\OrdersInfoViewApi;
use Uclient\Api\UserApi;

class CustomerInfoController extends AdminController{


    public function index(){

        $id = I('get.id',0);

        $result = apiCall(MemberApi::GET_INFO, array(array("uid"=>$id)));
        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->assign("userinfo",$result['info']);

        $result = apiCall(UserApi::GET_INFO, array($id));

        if($result['status']){
            if(!is_array($result['info'])){
                $this->error($result['info']);
            }
        }else{
            $this->error('未知错误');
        }

        $this->assign("useraccount",$result['info']);

        //默认地址查询
        $map = array('uid'=>$id);
        $result = apiCall(MemberConfigApi::GET_INFO,array($map));
        if(!$result['status']){
            $this->error('未知错误');
        }
        if($result['info']['default_address']!=0){
            $default_address = $result['info']['default_address'];
            //查询地址
            $result = apiCall(AddressApi::GET_INFO,array(array('id'=>$default_address)));

            if($result['status']){
                $this->assign('default_address',$result['info']);
            }
        }


        $this->display();

    }

    public function detail(){

    }


    public function Orders(){
        $arr = getDataRange(3);
        $userid = I('get.id', 0);
        if($userid == 0){
            $this->error('参数错误');
        }
        $payStatus = I('paystatus', '');
        $orderStatus = I('orderstatus', '');
        $order_code = I('post.order_code', '');
        $startdatetime = urldecode($arr[0]);
        //I('startdatetime', , 'urldecode');
        $enddatetime = urldecode($arr[1]);
        //I('enddatetime',   , 'urldecode');

        //分页时带参数get参数
        $params = array('startdatetime' => $startdatetime, 'enddatetime' => ($enddatetime),'wxaccountid'=>getWxAccountID());

        $startdatetime = strtotime($startdatetime);
        $enddatetime = strtotime($enddatetime);

        if ($startdatetime === FALSE || $enddatetime === FALSE) {
            LogRecord('INFO:' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this -> error(L('ERR_DATE_INVALID'));
        }

        $map = array();
        //$map['wxaccountid'] = getWxAccountID();
        if (!empty($order_code)) {
            $map['order_code'] = array('like', $order_code . '%');

        }
        if ($payStatus != '') {
            $map['pay_status'] = $payStatus;
            $params['paystatus'] = $payStatus;
        }
        if ($orderStatus != '') {
            $map['order_status'] = $orderStatus;
            $params['orderstatus'] = $orderStatus;
        }
        $map['createtime'] = array( array('EGT', $startdatetime), array('elt', $enddatetime), 'and');

        $page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
        $order = " createtime desc ";

        if ($userid > 0) {
            $map['uid'] = $userid;
        }

        //
        $result = apiCall(OrdersInfoViewApi::QUERY, array($map, $page, $order, $params));
        //dump($result);
        //
        if ($result['status']) {
            $this -> assign('order_code', $order_code);
            $this -> assign('orderStatus', $orderStatus);
            $this -> assign('payStatus', $payStatus);
            $this -> assign('startdatetime', $startdatetime);
            $this -> assign('enddatetime', $enddatetime);
            $this -> assign('list', $result['info']['list']);
            $this -> assign('show', $result['info']['show']);
            $this -> display();
        } else {
            LogRecord('INFO:' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this -> error($result['info']);
        }

    }

    public function scoreRecord(){

    }
    
}